package com.tianci.appstore.ui.list.manage;

import java.util.List;

import android.content.Context;
import android.view.KeyEvent;

import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.list.ListBlock.MuitlSelectionListener;
import com.tianci.appstore.ui.list.ListFlipper;
import com.tianci.appstore.ui.list.SkyListAdapter;
import com.tianci.appstore.ui.list.cgylist.ListOperationController;
import com.tianci.appstore.ui.widget.SkyAppPager.AnimListener;
import com.tianci.appstore.ui.widget.SkyBaseItemView.OnAppKeyListener;
import com.tianci.appstore.ui.widget.SkyCustomLayout;

public class ManageListController extends ListOperationController implements MuitlSelectionListener,
        OnAppKeyListener, AnimListener {
    protected List<SkyListAppData> dataList;
    private long mNeedSize = 0;
    private OnNeedSizeChangeListener mNeedSizeChangeListener = null;
    private SwitchFocusListener mSwitchFocusListener = null;

    public interface OnNeedSizeChangeListener {
        public boolean needSizeChange(long needSize);
    }

    public void setNeedSizeChangeListener(OnNeedSizeChangeListener listener) {
        this.mNeedSizeChangeListener = listener;
    }

    public interface SwitchFocusListener {
        public boolean switchFocus(int direction);
    }

    public void setSwitchFocusListener(SwitchFocusListener listener) {
        this.mSwitchFocusListener = listener;
    }

    public ManageListController(Context context, ListFlipper flipper, int row, int column) {
        super(context, flipper, row, column);
    }

    @Override
    public boolean onMultiSelectChanged(SkyListAppData data, boolean selected) {
        if (mNeedSizeChangeListener != null) {
            if (selected) {
                mNeedSize += data.getSize();
            } else {
                mNeedSize -= data.getSize();
                if (mNeedSize < 0) {
                    mNeedSize = 0;
                }
            }
            if (mNeedSizeChangeListener.needSizeChange(mNeedSize)) {
                return super.onMultiSelectChanged(data, selected);
            } else {
                return false;
            }
        }
        return super.onMultiSelectChanged(data, selected);
    }

    @Override
    public void clearSelectedDataList() {
        mNeedSize = 0;
        super.clearSelectedDataList();
    }

    @Override
    public boolean onKeyDown(int keyCode, int index, boolean[] turnPage) {
        // TODO Auto-generated method stub
        if (index % column == 0 && keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            if (mCurrentPage > 0) {
                direction = keyCode;
                int oldIndex = mPager.getDisplayedChild();
                mPager.displayPrePage();
                mCurrentPage--;
                posY = index / column;
                refreshPageNumber();

                if (mCurrentPage > 0 && mCurrentPage < mTotalPage - 1) {
                    int lastIndex = (oldIndex + 1) % ListFlipper.VIEW_PAGE_COUNT;
                    refreshPage(lastIndex, mCurrentPage - 1);
                }
                return true;
            }
//            else 
//                mPager.hitBoundary(keyCode);
        } else if (index % column == column - 1 && keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            if (mCurrentPage < mTotalPage - 1) {
                direction = keyCode;
                int oldIndex = mPager.getDisplayedChild();
                mPager.displayNextPage();
                mCurrentPage++;
                posY = index / column;
                refreshPageNumber();

                if (mCurrentPage > 0 && mCurrentPage < mTotalPage - 1) {
                    int firstIndex = (oldIndex + ListFlipper.VIEW_PAGE_COUNT - 1) % ListFlipper.VIEW_PAGE_COUNT;
                    refreshPage(firstIndex, mCurrentPage + 1);
                }
                return true;
            }
//            else 
//                mPager.hitBoundary(keyCode);
        }
        if (index < column && keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            if (mSwitchFocusListener != null)
                return mSwitchFocusListener.switchFocus(keyCode);
        } else if (index >= column * (row - 1) && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            if (mSwitchFocusListener != null)
                return mSwitchFocusListener.switchFocus(keyCode);
        }
        return false;
    }

    public void refreshPage(int childIndex, int pageIndex) {
        SkyCustomLayout child = (SkyCustomLayout) mPager.getChildAt(childIndex);
        SkyListAdapter adapter = (SkyListAdapter) child.getAdapter();
        adapter.setData(pageIndex, dataList.subList(pageSize * pageIndex, pageSize * pageIndex + getCountByIndex(pageIndex)));
    }

    public void setDataList(List<SkyListAppData> dataList) {
        this.dataList = dataList;
        initAppCount(dataList.size(), 0);

        SkyCustomLayout child = (SkyCustomLayout) mPager.getChildAt(0);
        SkyListAdapter adapter = (SkyListAdapter) child.getAdapter();
        adapter.setData(0, dataList.subList(0, getCountByIndex(0)));
        if (mTotalPage > 1) {
            SkyCustomLayout child2 = (SkyCustomLayout) mPager.getChildAt(1);
            SkyListAdapter adapter2 = (SkyListAdapter) child2.getAdapter();
            try {
                adapter2.setData(1, dataList.subList(pageSize, pageSize + getCountByIndex(1)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (mTotalPage > 2) {
            SkyCustomLayout child3 = (SkyCustomLayout) mPager.getChildAt(2);
            SkyListAdapter adapter3 = (SkyListAdapter) child3.getAdapter();
            adapter3.setData(2, dataList.subList(pageSize * 2, pageSize * 2 + getCountByIndex(2)));
        }
        mPager.setInAnimation(null);
        mPager.setOutAnimation(null);
        mPager.setDisplayedChild(0);
    }

    public void setDataList(List<SkyListAppData> dataList, int pageIndex, int focusIndex) {
        this.dataList = dataList;
        this.totalCount = dataList.size();
        this.mTotalPage = dataList.size() / (row * column);
        if (totalCount % (row * column) != 0)
            this.mTotalPage++;
        if (pageIndex >= mTotalPage) {
            pageIndex = mTotalPage - 1;
            focusIndex = pageSize - 1;
        }
        this.mCurrentPage = pageIndex;

        int displayChild = 1;
        int adjustPageIndex = pageIndex;
        if (pageIndex == 0) {
            displayChild = 0;
            adjustPageIndex++;
        } else if (pageIndex == mTotalPage - 1 && mTotalPage > 2) {
            displayChild = 2;
            adjustPageIndex--;
        }

        SkyCustomLayout child = (SkyCustomLayout) mPager.getChildAt(0);
        SkyListAdapter adapter = (SkyListAdapter) child.getAdapter();
        int page1Index = adjustPageIndex - 1;
        adapter.setData(page1Index, dataList.subList(page1Index, getCountByIndex(page1Index)));
        if (mTotalPage > 1) {
            int page2Index = adjustPageIndex;
            SkyCustomLayout child2 = (SkyCustomLayout) mPager.getChildAt(1);
            SkyListAdapter adapter2 = (SkyListAdapter) child2.getAdapter();
            adapter2.setData(page2Index, dataList.subList(pageSize * page2Index,
                    pageSize * page2Index + getCountByIndex(page2Index)));
        }
        if (mTotalPage > 2) {
            int page3Index = adjustPageIndex + 1;
            SkyCustomLayout child3 = (SkyCustomLayout) mPager.getChildAt(2);
            SkyListAdapter adapter3 = (SkyListAdapter) child3.getAdapter();
            adapter3.setData(page3Index, dataList.subList(pageSize * page3Index,
                    pageSize * page3Index + getCountByIndex(page3Index)));
        }

        mPager.setInAnimation(null);
        mPager.setOutAnimation(null);
        mPager.setDisplayedChild(displayChild);

        SkyCustomLayout displayChildView = (SkyCustomLayout) mPager.getChildAt(displayChild);
        if (mTotalPage >= displayChildView.getChildCount())
            focusIndex = displayChildView.getChildCount() - 1;
        displayChildView.setFocusPos(focusIndex, true);
        refreshPageNumber();
    }
}
